﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using DLLPC;
using DLLPC.Utilities;
using System.Windows.Forms;

namespace VisionCore
{
    public class EventHelper_RTC
    {
        private IOMap_RTC unitIO = null;
        private AliveBeatGenerate Alive_PCExist = new AliveBeatGenerate(1000, 1);
        public AliveBitDetective Alive_RemoteExist { get; set; } = new AliveBitDetective("Motion PC Alive", 2000);

        #region HSD(Remote)
        /// <summary>
        /// 对位结果
        /// </summary>
        public HandshakeDetective EfemAlignmentReportTrigger { get; set; } = new HandshakeDetective("铁框 对位结果");
        /// <summary>
        /// 读码结果
        /// </summary>
        public HandshakeDetective HSD_ExcSTReadCodeReportTrigger { get; set; } = new HandshakeDetective("倒膜平台 读码结果");
        /// <summary>
        /// 倒膜结果
        /// </summary>
        public HandshakeDetective HSD_ExcSTInspReportTrigger { get; set; } = new HandshakeDetective("倒腊平台 倒膜结果");
        /// <summary>
        /// 初对位结果
        /// </summary>
        public HandshakeDetective HSD_SortSTFirstAlignmentReportTrigger { get; set; } = new HandshakeDetective("挑拣平台 初对位结果");
        /// <summary>
        /// 精对位结果
        /// </summary>
        public HandshakeDetective HSD_SortSTFinalAlignmentReportTrigger { get; set; } = new HandshakeDetective("挑拣平台 精对位结果");
        /// <summary>
        /// 上侧对位结果
        /// </summary>
        public HandshakeDetective HSD_DieAlignmentUpReportTrigger { get; set; } = new HandshakeDetective("晶粒 上侧对位结果");
        /// <summary>
        /// 前侧对位结果
        /// </summary>
        public HandshakeDetective HSD_DieAlignmentFrontReportTrigger { get; set; } = new HandshakeDetective("晶粒 前侧对位结果");
        /// <summary>
        /// 检查结果
        /// </summary>
        public HandshakeDetective HSD_LaserCodeReportTrigger { get; set; } = new HandshakeDetective("镭射码 检查结果");
        /// <summary>
        /// 检查结果
        /// </summary>
        public HandshakeDetective HSD_ULDTR1InspReportTrigger { get; set; } = new HandshakeDetective("出料移载-1 检查结果");
        /// <summary>
        /// 检查结果
        /// </summary>
        public HandshakeDetective HSD_ULDTR2InspReportTrigger { get; set; } = new HandshakeDetective("出料移载-2 检查结果");
        /// <summary>
        /// 初对位结果
        /// </summary>
        public HandshakeDetective HSD_ULDSTFirstAlignmentReportTrigger { get; set; } = new HandshakeDetective("载出平台 初对位结果");
        /// <summary>
        /// 精对位结果
        /// </summary>
        public HandshakeDetective HSD_ULDSTFinalAlignmentReportTrigger { get; set; } = new HandshakeDetective("载出平台 精对位结果");

        /// <summary>
        /// 载出对位前触发回复结果
        /// </summary>
        public HandshakeDetective HSD_ULDTotalTrigerReportTrigger { get; set; } = new HandshakeDetective("载出 对位前触发结果");

        public HandshakeDetective HSD_EfemXControlTrigger { get; set; } = new HandshakeDetective("EfemX轴控制触发");
        public HandshakeDetective HSD_ExcSTControlTrigger { get; set; } = new HandshakeDetective("倒膜平台控制触发");
        public HandshakeDetective HSD_ConstantControlTrigger { get; set; } = new HandshakeDetective("通用控制触发");
        public HandshakeDetective HSD_SortSTControlTrigger { get; set; } = new HandshakeDetective("挑拣平台控制触发");
        public HandshakeDetective HSD_ULDSTControlTrigger { get; set; } = new HandshakeDetective("载出平台控制触发");

        public HandshakeDetective HSD_CCD1_XControlTrigger { get; set; } = new HandshakeDetective("CCD1_X轴控制触发");
        public HandshakeDetective HSD_CCD1_YControlTrigger { get; set; } = new HandshakeDetective("CCD1_Y轴控制触发");
        public HandshakeDetective HSD_CCD1_ZControlTrigger { get; set; } = new HandshakeDetective("CCD1_Z轴控制触发");
        public HandshakeDetective HSD_CCD2_XControlTrigger { get; set; } = new HandshakeDetective("CCD1_X轴控制触发");
        public HandshakeDetective HSD_CCD2_YControlTrigger { get; set; } = new HandshakeDetective("CCD1_Y轴控制触发");
        public HandshakeDetective HSD_CCD2_ZControlTrigger { get; set; } = new HandshakeDetective("CCD1_Z轴控制触发");
        public HandshakeDetective HSD_CCD1InspReportTrigger { get; set; } = new HandshakeDetective("CCD1 检查结果");
        public HandshakeDetective HSD_CCD2InspReportTrigger { get; set; } = new HandshakeDetective("CCD2 检查结果");
        public HandshakeDetective HSD_CCD1LocateReportTrigger { get; set; } = new HandshakeDetective("CCD1 对位结果");
        public HandshakeDetective HSD_CCD2LocateReportTrigger { get; set; } = new HandshakeDetective("CCD2 对位结果");




        #endregion

        #region HSG(Local)

        public HandshakeGenerate HSG_CCD1AlignmentTrigger{ get; set; } = new HandshakeGenerate("CCD1对位触发");
        public HandshakeGenerate HSG_CCD2AlignmentTrigger{ get; set; } = new HandshakeGenerate("CCD2对位触发");
        public HandshakeGenerate HSG_CCD1InspTrigger{ get; set; } = new HandshakeGenerate("CCD1检测触发");
        public HandshakeGenerate HSG_CCD2InspTrigger { get; set; } = new HandshakeGenerate("CCD2检测触发");
        public HandshakeGenerate HSG_RecipeChangeTrigger { get; set; } = new HandshakeGenerate("工单切换触发");

        public HandshakeGenerate HSG_EfemAlignmentTrigger { get; set; } = new HandshakeGenerate("铁框 大视野对位触发");
        public HandshakeGenerate HSG_ExcSTReadCodeTrigger { get; set; } = new HandshakeGenerate("倒膜平台 读码触发");
        public HandshakeGenerate HSG_ExcSTInspTrigger { get; set; } = new HandshakeGenerate("倒膜平台 倒膜结果检测触发");
        public HandshakeGenerate HSG_SortSTFirstAlignmentTrigger { get; set; } = new HandshakeGenerate("挑拣平台 初对位触发");
        public HandshakeGenerate HSG_SortSTFinalAlignmentTrigger { get; set; } = new HandshakeGenerate("挑拣平台 精对位触发");
        public HandshakeGenerate HSG_DieAlignmentUpTrigger { get; set; } = new HandshakeGenerate("晶粒 上侧对位触发");
        public HandshakeGenerate HSG_DieAlignmentFrontTrigger { get; set; } = new HandshakeGenerate("晶粒 前侧对位触发");
      
        public HandshakeGenerate HSG_LaserCodeReadTrigger { get; set; } = new HandshakeGenerate("镭射码 检查触发");
        public HandshakeGenerate HSG_ULDTR1InspTrigger { get; set; } = new HandshakeGenerate("出料移载-1 检查触发");
        public HandshakeGenerate HSG_ULDTR2InspTrigger { get; set; } = new HandshakeGenerate("出料移载-2 检查触发");
        public HandshakeGenerate HSG_ULDSTFirstAlignmentTrigger { get; set; } = new HandshakeGenerate("载出平台 初对位触发");
        public HandshakeGenerate HSG_ULDSTFinalAlignmentTrigger { get; set; } = new HandshakeGenerate("载出平台 精对位触发");
        /// <summary>
        /// 载出触发前Total触发
        /// </summary>
        public HandshakeGenerate HSG_ULDTotalTrigger { get; set; } = new HandshakeGenerate("载出 对位前触发");
        public byte CameraRealATimeFlag { get; set; } = 0;
        public byte CameraRealBTimeFlag { get; set; } = 0;

        #endregion

        internal void UpdateRemoteFlag(ushort[] remoteDataBuf)
        {
            Alive_RemoteExist.AliveBeat = MemoryAccess.GetBit(remoteDataBuf, 1, 0);

            HSD_CCD1_XControlTrigger.TrigBitValue = MemoryAccess.GetBit(remoteDataBuf, 1, 1);
            HSD_CCD1_YControlTrigger.TrigBitValue = MemoryAccess.GetBit(remoteDataBuf, 1, 2);
            HSD_CCD1_ZControlTrigger.TrigBitValue = MemoryAccess.GetBit(remoteDataBuf, 1, 3);
            HSD_CCD2_XControlTrigger.TrigBitValue = MemoryAccess.GetBit(remoteDataBuf, 1, 4);
            HSD_CCD2_YControlTrigger.TrigBitValue = MemoryAccess.GetBit(remoteDataBuf, 1, 5);
            HSD_CCD2_ZControlTrigger.TrigBitValue = MemoryAccess.GetBit(remoteDataBuf, 1, 6);
            HSD_CCD1InspReportTrigger.TrigBitValue = MemoryAccess.GetBit(remoteDataBuf, 1, 7);
            HSD_CCD2InspReportTrigger.TrigBitValue = MemoryAccess.GetBit(remoteDataBuf, 1, 8);
            HSD_CCD1LocateReportTrigger.TrigBitValue = MemoryAccess.GetBit(remoteDataBuf, 1, 9);
            HSD_CCD2LocateReportTrigger.TrigBitValue = MemoryAccess.GetBit(remoteDataBuf, 1, 10);

            //HSD_ExcSTReadCodeReportTrigger.TrigBitValue = MemoryAccess.GetBit(remoteDataBuf, 10, 4);
            //HSD_ExcSTInspReportTrigger.TrigBitValue = MemoryAccess.GetBit(remoteDataBuf, 10, 6);
            //HSD_SortSTFirstAlignmentReportTrigger.TrigBitValue = MemoryAccess.GetBit(remoteDataBuf, 10, 8);
            //HSD_SortSTFinalAlignmentReportTrigger.TrigBitValue = MemoryAccess.GetBit(remoteDataBuf, 10, 10);
            //HSD_DieAlignmentUpReportTrigger.TrigBitValue = MemoryAccess.GetBit(remoteDataBuf, 10, 12);
            //HSD_DieAlignmentFrontReportTrigger.TrigBitValue = MemoryAccess.GetBit(remoteDataBuf, 10, 14);

            //HSD_LaserCodeReportTrigger.TrigBitValue = MemoryAccess.GetBit(remoteDataBuf, 11, 2);
            //HSD_ULDTR1InspReportTrigger.TrigBitValue = MemoryAccess.GetBit(remoteDataBuf, 11, 4);
            //HSD_ULDTR2InspReportTrigger.TrigBitValue = MemoryAccess.GetBit(remoteDataBuf, 11, 6);
            //HSD_ULDSTFirstAlignmentReportTrigger.TrigBitValue = MemoryAccess.GetBit(remoteDataBuf, 11, 8);
            //HSD_ULDSTFinalAlignmentReportTrigger.TrigBitValue = MemoryAccess.GetBit(remoteDataBuf, 11, 10);

            //HSD_EfemXControlTrigger.TrigBitValue = MemoryAccess.GetBit(remoteDataBuf, 12, 0);
            //HSD_ExcSTControlTrigger.TrigBitValue = MemoryAccess.GetBit(remoteDataBuf, 12, 2);
            //HSD_ConstantControlTrigger.TrigBitValue = MemoryAccess.GetBit(remoteDataBuf, 12, 4);
            //HSD_SortSTControlTrigger.TrigBitValue = MemoryAccess.GetBit(remoteDataBuf, 12, 6);
            //HSD_ULDSTControlTrigger.TrigBitValue = MemoryAccess.GetBit(remoteDataBuf, 12, 8);

            //HSG_EfemAlignmentTrigger.ReplyBitValue = MemoryAccess.GetBit(remoteDataBuf, 0, 7);
            //HSG_ExcSTReadCodeTrigger.ReplyBitValue = MemoryAccess.GetBit(remoteDataBuf, 0, 4);
            //HSG_ExcSTInspTrigger.ReplyBitValue = MemoryAccess.GetBit(remoteDataBuf, 0, 6);

            //HSG_SortSTFirstAlignmentTrigger.ReplyBitValue = MemoryAccess.GetBit(remoteDataBuf, 0, 8);
            //HSG_SortSTFinalAlignmentTrigger.ReplyBitValue = MemoryAccess.GetBit(remoteDataBuf, 0, 10);
            //HSG_DieAlignmentUpTrigger.ReplyBitValue = MemoryAccess.GetBit(remoteDataBuf, 0, 12);
            //HSG_DieAlignmentFrontTrigger.ReplyBitValue = MemoryAccess.GetBit(remoteDataBuf, 0, 14);
            //HSG_RecipeChangeTrigger.ReplyBitValue = MemoryAccess.GetBit(remoteDataBuf, 1, 0);
            //HSG_LaserCodeReadTrigger.ReplyBitValue = MemoryAccess.GetBit(remoteDataBuf, 1, 2);
            //HSG_ULDTR1InspTrigger.ReplyBitValue = MemoryAccess.GetBit(remoteDataBuf, 1, 4);
            //HSG_ULDTR2InspTrigger.ReplyBitValue = MemoryAccess.GetBit(remoteDataBuf, 1, 6);
            //HSG_ULDSTFirstAlignmentTrigger.ReplyBitValue = MemoryAccess.GetBit(remoteDataBuf, 1, 8);
            //HSG_ULDSTFinalAlignmentTrigger.ReplyBitValue = MemoryAccess.GetBit(remoteDataBuf, 1, 10);
            //HSG_ULDTotalTrigger.ReplyBitValue = MemoryAccess.GetBit(remoteDataBuf, 1, 11);

            if (HSG_ULDTotalTrigger.ReplyBitValue==1)
            {
                ProcedureHelper_Exc.ULDTotalTrigered = true;
            }
        }

        internal void UpdateLocalFlag(ref ushort[] localDataBuf)
        {
            MemoryAccess.SetBit(ref localDataBuf, Alive_PCExist.AliveBeat, 0, 0);
            MemoryAccess.SetBit(ref localDataBuf, (byte)(ProcedureHelper_EQ.AutoMode?1:0), 0, 1);
            MemoryAccess.SetBit(ref localDataBuf, HSG_RecipeChangeTrigger.TrigBitValue, 1, 0);
                        
            MemoryAccess.SetBit(ref localDataBuf, HSG_CCD1AlignmentTrigger.ReplyBitValue, 1, 1);
            MemoryAccess.SetBit(ref localDataBuf, HSG_CCD2AlignmentTrigger.ReplyBitValue, 1, 2);
            MemoryAccess.SetBit(ref localDataBuf, HSG_CCD1InspTrigger.ReplyBitValue, 10, 3);
            MemoryAccess.SetBit(ref localDataBuf, HSG_CCD2InspTrigger.ReplyBitValue, 10, 4);

            //MemoryAccess.SetBit(ref localDataBuf, HSD_SortSTFirstAlignmentReportTrigger.ReplyBitValue, 10, 8);
            //MemoryAccess.SetBit(ref localDataBuf, HSD_SortSTFinalAlignmentReportTrigger.ReplyBitValue, 10, 10);
            //MemoryAccess.SetBit(ref localDataBuf, HSD_DieAlignmentUpReportTrigger.ReplyBitValue, 10, 12);
            //MemoryAccess.SetBit(ref localDataBuf, HSD_DieAlignmentFrontReportTrigger.ReplyBitValue, 10, 14);

            //MemoryAccess.SetBit(ref localDataBuf, HSD_LaserCodeReportTrigger.ReplyBitValue, 11, 2);
            //MemoryAccess.SetBit(ref localDataBuf, HSD_ULDTR1InspReportTrigger.ReplyBitValue, 11, 4);
            //MemoryAccess.SetBit(ref localDataBuf, HSD_ULDTR2InspReportTrigger.ReplyBitValue, 11, 6);
            //MemoryAccess.SetBit(ref localDataBuf, HSD_ULDSTFirstAlignmentReportTrigger.ReplyBitValue, 11, 8);
            //MemoryAccess.SetBit(ref localDataBuf, HSD_ULDSTFinalAlignmentReportTrigger.ReplyBitValue, 11, 10);

            //MemoryAccess.SetBit(ref localDataBuf, HSD_EfemXControlTrigger.ReplyBitValue, 12, 0);
            //MemoryAccess.SetBit(ref localDataBuf, HSD_ExcSTControlTrigger.ReplyBitValue, 12, 2);
            //MemoryAccess.SetBit(ref localDataBuf, HSD_ConstantControlTrigger.ReplyBitValue, 12, 4);
            //MemoryAccess.SetBit(ref localDataBuf, HSD_SortSTControlTrigger.ReplyBitValue, 12, 6);
            //MemoryAccess.SetBit(ref localDataBuf, HSD_ULDSTControlTrigger.ReplyBitValue, 12, 8);

            //MemoryAccess.SetBit(ref localDataBuf, HSG_EfemAlignmentTrigger.TrigBitValue, 0, 2);
            //MemoryAccess.SetBit(ref localDataBuf, HSG_ExcSTReadCodeTrigger.TrigBitValue, 0, 4);
            //MemoryAccess.SetBit(ref localDataBuf, HSG_ExcSTInspTrigger.TrigBitValue, 0, 6);

            //MemoryAccess.SetBit(ref localDataBuf, CameraRealATimeFlag, 1, 0);
            //MemoryAccess.SetBit(ref localDataBuf, CameraRealBTimeFlag, 1, 1);
            //MemoryAccess.SetBit(ref localDataBuf, HSG_SortSTFirstAlignmentTrigger.TrigBitValue, 0, 8);
            //MemoryAccess.SetBit(ref localDataBuf, HSG_SortSTFinalAlignmentTrigger.TrigBitValue, 0, 10);
            //MemoryAccess.SetBit(ref localDataBuf, HSG_DieAlignmentUpTrigger.TrigBitValue, 0, 12);
            //MemoryAccess.SetBit(ref localDataBuf, HSG_DieAlignmentFrontTrigger.TrigBitValue, 0, 14);

            //MemoryAccess.SetBit(ref localDataBuf, HSG_LaserCodeReadTrigger.TrigBitValue, 1, 2);
            //MemoryAccess.SetBit(ref localDataBuf, HSG_ULDTR1InspTrigger.TrigBitValue, 1, 4);
            //MemoryAccess.SetBit(ref localDataBuf, HSG_ULDTR2InspTrigger.TrigBitValue, 1, 6);
            //MemoryAccess.SetBit(ref localDataBuf, HSG_ULDSTFirstAlignmentTrigger.TrigBitValue, 1, 8);
            //MemoryAccess.SetBit(ref localDataBuf, HSG_ULDSTFinalAlignmentTrigger.TrigBitValue, 1, 10);
            //MemoryAccess.SetBit(ref localDataBuf, HSG_ULDTotalTrigger.TrigBitValue, 1, 11);
        }

        private Thread pollingthread;
        private static bool blnRun = true;
        public void Start()
        {
            pollingthread = new Thread(threadPolling);
            pollingthread.IsBackground = true;
            pollingthread.Start();
        }

        public void Quit()
        {
            blnRun = false;
        }

        private void threadPolling()
        {
            SystemTimeCounter sc300ms = new SystemTimeCounter();
            Thread.Sleep(1000);
            AttachEvent();

            while (blnRun)
            {
                Alive_PCExist.Monitor();
                Alive_RemoteExist.Monitor();

                HSD_CCD1_XControlTrigger.Monitor();
                HSD_CCD1_YControlTrigger.Monitor();
                HSD_CCD1_ZControlTrigger.Monitor();
                HSD_CCD2_XControlTrigger.Monitor();
                HSD_CCD2_YControlTrigger.Monitor();
                HSD_CCD2_ZControlTrigger.Monitor();

                HSD_CCD1InspReportTrigger.Monitor();
                HSD_CCD2InspReportTrigger.Monitor();
                HSD_CCD1LocateReportTrigger.Monitor();
                HSD_CCD2LocateReportTrigger.Monitor();

                HSG_RecipeChangeTrigger.Monitor();               

                SystemTimeCounter.timeBeginPeriod(1);
                Thread.Sleep(1);
                SystemTimeCounter.timeEndPeriod(1);
            }
        }

        /// <summary>
        /// 交换信号 与 事件关联
        /// 触发信号与事件关联
        /// </summary>
        private void AttachEvent()
        {
            HSD_CCD1_XControlTrigger.TriggerOccour = new EventHandler(CCD1_XControlRequest);
            HSD_CCD1_YControlTrigger.TriggerOccour = new EventHandler(CCD1_YControlRequest);
            HSD_CCD1_ZControlTrigger.TriggerOccour = new EventHandler(CCD1_ZControlRequest);
            HSD_CCD2_XControlTrigger.TriggerOccour = new EventHandler(CCD2_XControlRequest);
            HSD_CCD2_YControlTrigger.TriggerOccour = new EventHandler(CCD2_YControlRequest);
            HSD_CCD2_ZControlTrigger.TriggerOccour = new EventHandler(CCD2_ZControlRequest);
            HSD_CCD1InspReportTrigger.TriggerOccour = new EventHandler(ULDSTControlRequest);
            HSD_CCD2InspReportTrigger.TriggerOccour = new EventHandler(ULDSTControlRequest);
            HSD_CCD1LocateReportTrigger.TriggerOccour = new EventHandler(ULDSTControlRequest);
            HSD_CCD2LocateReportTrigger.TriggerOccour = new EventHandler(ULDSTControlRequest);
           }

        private void EfemAlignRequest(object obj, EventArgs e)
        {
            if (RTCHelper.DIO.R_EfemAlignRet == 1)
            {
                ProcedureHelper_Exc.EfemAlignOK = true;
                ProcedureHelper_Exc.EfemAlignOffset_X = RTCHelper.DIO.R_EfemOffset_X;
                ProcedureHelper_Exc.EfemAlignOffset_Y = RTCHelper.DIO.R_EfemOffset_Y;
                ProcedureHelper_Exc.EfemAlignOffset_R = RTCHelper.DIO.R_EfemOffset_R;
            }
            else
            {
                ProcedureHelper_Exc.EfemAlignOK = false;
                ProcedureHelper_Exc.EfemAlignOffset_X = 0;
                ProcedureHelper_Exc.EfemAlignOffset_Y = 0;
                ProcedureHelper_Exc.EfemAlignOffset_R = 0;
            }
            ProcedureHelper_Exc.LogMsg(string.Format("Efem对位相机回复：结果[1:OK]:{0}，X:{1:F3}，Y:{2:F3}，R:{3:F3}", RTCHelper.DIO.R_EfemAlignRet, RTCHelper.DIO.R_EfemOffset_X, RTCHelper.DIO.R_EfemOffset_Y, RTCHelper.DIO.R_EfemOffset_R));
        }

     

        private void SortSTFirstAlignRequest(object obj, EventArgs e)
        {
            if (RTCHelper.DIO.R_SortSTFirstAlignRet == 1)
            {
                ProcedureHelper_Exc.SortSTFirstAlignOK = true;
            }
            else
            {
                ProcedureHelper_Exc.SortSTFirstAlignOK = false;
            }
            ProcedureHelper_Exc.LogMsg(string.Format("挑拣平台初对位相机回复：结果[1:OK]:{0}", RTCHelper.DIO.R_SortSTFirstAlignRet));
        }

        private void SortSTFinalAlignRequest(object obj, EventArgs e)
        {
            if (RTCHelper.DIO.R_SortSTFinalAlignRet == 1)
            {
                ProcedureHelper_Exc.SortSTFinalAlignOK = true;
                ProcedureHelper_Exc.SortSTFinalAlignForTR1_X = RTCHelper.DIO.R_SortSTFinalForTR1_X;
                ProcedureHelper_Exc.SortSTFinalAlignForTR1_Y = RTCHelper.DIO.R_SortSTFinalForTR1_Y;
                ProcedureHelper_Exc.SortSTFinalAlignForTR1_R = RTCHelper.DIO.R_SortSTFinalForTR1_R;
                ProcedureHelper_Exc.SortSTFinalAlignForTR1_StageR = RTCHelper.DIO.R_SortSTFinalForTR1_StageR;

                ProcedureHelper_Exc.SortSTFinalAlignForTR2_X = RTCHelper.DIO.R_SortSTFinalForTR2_X;
                ProcedureHelper_Exc.SortSTFinalAlignForTR2_Y = RTCHelper.DIO.R_SortSTFinalForTR2_Y;
                ProcedureHelper_Exc.SortSTFinalAlignForTR2_R = RTCHelper.DIO.R_SortSTFinalForTR2_R;
                ProcedureHelper_Exc.SortSTFinalAlignForTR2_StageR = RTCHelper.DIO.R_SortSTFinalForTR2_StageR;
            }
            else
            {
                ProcedureHelper_Exc.SortSTFinalAlignOK = false;
                //ProcedureHelper_Exc.SortSTFinalAlignForTR1_X = 0;
                //ProcedureHelper_Exc.SortSTFinalAlignForTR1_Y = 0;
                //ProcedureHelper_Exc.SortSTFinalAlignForTR1_R = 0;
                                                               
                //ProcedureHelper_Exc.SortSTFinalAlignForTR2_X = 0;
                //ProcedureHelper_Exc.SortSTFinalAlignForTR2_Y = 0;
                //ProcedureHelper_Exc.SortSTFinalAlignForTR2_R = 0;
            }
            ProcedureHelper_Exc.LogMsg(string.Format("挑拣平台精对位相机回复：结果[1:OK]:{0}，TR1_X:{1:F3}，TR1_Y:{2:F3}，TR1_R:{3:F3}，TR2_X:{4:F3}，TR2_Y:{5:F3}，TR2_R:{6:F3}", RTCHelper.DIO.R_SortSTFinalAlignRet, RTCHelper.DIO.R_SortSTFinalForTR1_X, RTCHelper.DIO.R_SortSTFinalForTR1_Y, RTCHelper.DIO.R_SortSTFinalForTR1_R, RTCHelper.DIO.R_SortSTFinalForTR2_X, RTCHelper.DIO.R_SortSTFinalForTR2_Y, RTCHelper.DIO.R_SortSTFinalForTR2_R));
        }

        private void DieUpAlignRequest(object obj, EventArgs e)
        {
            ProcedureHelper_Exc.DieUpInsped = true;
            if (RTCHelper.DIO.R_DieAlignUpRet == 1)
            {
                ProcedureHelper_Exc.DieUpAlignOK = true;
                ProcedureHelper_Exc.DieOffset_Z = RTCHelper.DIO.R_LaserSTOffset_Z;
            }
            else
            {
                ProcedureHelper_Exc.DieUpAlignOK = false;
                ProcedureHelper_Exc.DieOffset_Z = 0;
            }
            ProcedureHelper_Exc.LogMsg(string.Format("镭射平台上对位相机回复：结果[1:OK]:{0}，Z:{1:F3}", RTCHelper.DIO.R_DieAlignUpRet, RTCHelper.DIO.R_LaserSTOffset_Z));
        }

        private void DieFrontAlignRequest(object obj, EventArgs e)
        {
            ProcedureHelper_Exc.DieFrontInsped = true;
            if (RTCHelper.DIO.R_DieAlignFrontRet == 1)
            {
                ProcedureHelper_Exc.DieFrontAlignOk = true;
                ProcedureHelper_Exc.DieOffset_X = RTCHelper.DIO.R_LaserSTOffset_X;
                ProcedureHelper_Exc.DieOffset_Y = RTCHelper.DIO.R_LaserSTOffset_Y;
            }
            else
            {
                ProcedureHelper_Exc.DieFrontAlignOk = false;
                ProcedureHelper_Exc.DieOffset_X = 0;
                ProcedureHelper_Exc.DieOffset_Y = 0;
            }
            ProcedureHelper_Exc.LogMsg(string.Format("镭射平台侧对位相机回复：结果[1:OK]:{0}，X:{1:F3}，Y:{2:F3}", RTCHelper.DIO.R_DieAlignFrontRet, RTCHelper.DIO.R_LaserSTOffset_X, RTCHelper.DIO.R_LaserSTOffset_Y));


            
        }


        private void ULDTR_2_InspRequest(object obj, EventArgs e)
        {
            ProcedureHelper_Exc.ULDTR_2_Insped = true;
            if (RTCHelper.DIO.R_ULDTR_2_InspRet == 1 && (RTCHelper.DIO.R_ULDTR_1_AlignRet == 1 || true))
            {
                ProcedureHelper_Exc.ULDTR_2_AlignOk = true;
                ProcedureHelper_Exc.ULDTR_2_InspRet = 1;
                ProcedureHelper_Exc.ULDTR_2_Offset_X = RTCHelper.DIO.R_ULDTR_2_Offset_X;
                ProcedureHelper_Exc.ULDTR_2_Offset_Y = RTCHelper.DIO.R_ULDTR_2_Offset_Y;
                ProcedureHelper_Exc.ULDTR_2_Offset_R = RTCHelper.DIO.R_ULDTR_2_Offset_R;
            }
            else
            {
                ProcedureHelper_Exc.ULDTR_2_Offset_X = 0;
                ProcedureHelper_Exc.ULDTR_2_Offset_Y = 0;
                ProcedureHelper_Exc.ULDTR_2_Offset_R = 0;
                //if (RTCHelper.DIO.R_ULDTR_2_InspRet != 1)
                //{
                    ProcedureHelper_Exc.ULDTR_2_InspRet = 2;
                //}
            }
            ProcedureHelper_Exc.LogMsg(string.Format("载出移载TR2对位相机回复：结果[1:OK]:{0}，X:{1:F3}，Y:{2:F3}", RTCHelper.DIO.R_ULDTR_2_InspRet, RTCHelper.DIO.R_ULDTR_2_Offset_X, RTCHelper.DIO.R_ULDTR_2_Offset_Y));
        }

        private void ULDSTFirstAlignRequest(object obj, EventArgs e)
        {
            int idx = RTCHelper.DIO.L_ULDST_AcqIdx - 1;
            if (idx<0)
            {
                idx = 0;
            }
            PointRotation.Point p = new PointRotation.Point();
            if (RTCHelper.DIO.R_ULDSTFirstAlignRet == 1)
            {
                Kernel.MarkLocate_ULDST.Marks[idx].Finded = true;
                p.x = Kernel.MarkLocate_ULDST.Marks[idx].Mark.x - RTCHelper.DIO.R_ULDSTFirstOffset_X;
                p.y = Kernel.MarkLocate_ULDST.Marks[idx].Mark.y - RTCHelper.DIO.R_ULDSTFirstOffset_Y;
                Kernel.MarkLocate_ULDST.Marks[idx].PMark = p;
            }
            else
            {
                Kernel.MarkLocate_ULDST.Marks[idx].Finded = false;
                p.x = Kernel.MarkLocate_ULDST.Marks[idx].Mark.x;
                p.y = Kernel.MarkLocate_ULDST.Marks[idx].Mark.y;
                Kernel.MarkLocate_ULDST.Marks[idx].PMark = p;
            }
            ProcedureHelper_Exc.LogMsg(string.Format("载出平台初对位相机回复：结果[1:OK]:{0}，X:{1:F3}，Y:{2:F3}", RTCHelper.DIO.R_ULDSTFirstAlignRet, RTCHelper.DIO.R_ULDSTFirstOffset_X, RTCHelper.DIO.R_ULDSTFirstOffset_Y));
        }




        private void CCD1_XControlRequest(object obj, EventArgs e)
        {
            if (Math.Abs(RTCHelper.DIO.R_CCD1Control_X - Kernel.motion.CCD1_X.CurrentCmdPos) > 10) return;

            double dbl = RTCHelper.DIO.R_CCD1Control_X;
            Kernel.motion.CCD1_X.AxisMoveAbs(dbl, 1, 30, 1, 1);

            ProcedureHelper_Exc.LogMotionMsg(string.Format("视觉触发：CCD1,X移动到 {0:F3} 触发", dbl));
        }
        private void CCD1_YControlRequest(object obj, EventArgs e)
        {
            if (Math.Abs(RTCHelper.DIO.R_CCD1Control_Y - Kernel.motion.CCD1_Y.CurrentCmdPos) > 10) return;

            double dbl = RTCHelper.DIO.R_CCD1Control_Y;
            Kernel.motion.CCD1_Y.AxisMoveAbs(dbl, 1, 30, 1, 1);

            ProcedureHelper_Exc.LogMotionMsg(string.Format("视觉触发：CCD1,Y移动到 {0:F3} 触发", dbl));
        }
        private void CCD1_ZControlRequest(object obj, EventArgs e)
        {
            if (Math.Abs(RTCHelper.DIO.R_CCD1Control_Z - Kernel.motion.CCD1_Z.CurrentCmdPos) > 10) return;

            double dbl = RTCHelper.DIO.R_CCD1Control_Z;
            Kernel.motion.CCD1_Z.AxisMoveAbs(dbl, 1, 10, 1, 1);

            ProcedureHelper_Exc.LogMotionMsg(string.Format("视觉触发：CCD1,Z移动到 {0:F3} 触发", dbl));
        }
        private void CCD2_XControlRequest(object obj, EventArgs e)
        {
            if (Math.Abs(RTCHelper.DIO.R_CCD2Control_X - Kernel.motion.CCD2_X.CurrentCmdPos) > 10) return;

            double dbl = RTCHelper.DIO.R_CCD2Control_X;
            Kernel.motion.CCD2_X.AxisMoveAbs(dbl, 1, 30, 1, 1);

            ProcedureHelper_Exc.LogMotionMsg(string.Format("视觉触发：CCD2,X移动到 {0:F3} 触发", dbl));
        }
        private void CCD2_YControlRequest(object obj, EventArgs e)
        {
            if (Math.Abs(RTCHelper.DIO.R_CCD2Control_Y - Kernel.motion.CCD2_Y.CurrentCmdPos) > 10) return;

            double dbl = RTCHelper.DIO.R_CCD2Control_Y;
            Kernel.motion.CCD2_Y.AxisMoveAbs(dbl, 1, 30, 1, 1);

            ProcedureHelper_Exc.LogMotionMsg(string.Format("视觉触发：CCD2,Y移动到 {0:F3} 触发", dbl));
        }
        private void CCD2_ZControlRequest(object obj, EventArgs e)
        {
            if (Math.Abs(RTCHelper.DIO.R_CCD2Control_Z - Kernel.motion.CCD2_Z.CurrentCmdPos) > 10) return;

            double dbl = RTCHelper.DIO.R_CCD2Control_Z;
            Kernel.motion.CCD2_Z.AxisMoveAbs(dbl, 1, 10, 1, 1);

            ProcedureHelper_Exc.LogMotionMsg(string.Format("视觉触发：CCD2,Z移动到 {0:F3} 触发", dbl));
        }
        private void ExcSTControlRequest(object obj, EventArgs e)
         {
            if (Math.Abs(RTCHelper.DIO.R_ExcSTControl_X - Kernel.motion.ExcCCD.CurrentCmdPos) > 50 || Math.Abs(RTCHelper.DIO.R_ExcSTControl_Y - Kernel.motion.ExcST_Y.CurrentCmdPos) > 50 || Math.Abs(RTCHelper.DIO.R_ExcSTControl_R - Kernel.motion.ExcST_R.CurrentCmdPos) > 5) return;
            double dblX = RTCHelper.DIO.R_ExcSTControl_X;
            double dblY = RTCHelper.DIO.R_ExcSTControl_Y;
            double dblR = RTCHelper.DIO.R_ExcSTControl_R;
            Kernel.motion.ExcCCD.AxisMoveAbs(dblX, 0.5, 10);
            if (ProcedureHelper_Exc.ExcST_AllowMove)
            {
                Kernel.motion.ExcST_Y.AxisMoveAbs(dblY, 0.5, 10);
                Kernel.motion.ExcST_R.AxisMoveAbs(dblR, 0.5, 10);
            }
            ProcedureHelper_Exc.LogMotionMsg(string.Format("视觉触发：倒膜平台,X移动到 {0:F3} 触发,Y移动到 {1:F3} 触发,R移动到 {2:F3} 触发", dblX, dblY, dblR));
        }

     
        private void ULDSTControlRequest(object obj, EventArgs e)
        {
            if (Math.Abs(RTCHelper.DIO.R_ULDSTControl_X - Kernel.motion.ULDST_X.CurrentCmdPos) > 10 || Math.Abs(RTCHelper.DIO.R_ULDSTControl_Y - Kernel.motion.ULDST_Y.CurrentCmdPos) > 10 || Math.Abs(RTCHelper.DIO.R_ULDSTControl_R) > 5) return;

            double dblX = RTCHelper.DIO.R_ULDSTControl_X;
            double dblY = RTCHelper.DIO.R_ULDSTControl_Y;
            if (ProcedureHelper_Exc.ULDST_AllowHome)
            {
                Kernel.motion.ULDST_X.AxisMoveAbs(dblX, 0.5, 10);
                Kernel.motion.ULDST_Y.AxisMoveAbs(dblY, 0.5, 10);
            }
            ProcedureHelper_Exc.LogMotionMsg(string.Format("视觉触发：ULDST,X移动到 {0:F3} 触发,Y移动到 {1:F3} 触发", dblX, dblY));
        }

        public enum Constant_Control
        {
            auto = 0,
            manul = 1,
            home = 2,

            /// <summary>
            /// SortST 粗定位
            /// </summary>
            Locate_Cu = 5,
            /// <summary>
            /// SortST 精定位
            /// </summary>
            Locate_Jing = 6,
            /// <summary>
            /// LaserST 上侧
            /// </summary>
            Label_Up = 7,
            /// <summary>
            /// LaserST 前侧
            /// </summary>
            Label_Side = 8,
            /// <summary>
            /// Laser 读码
            /// </summary>
            Label_OCR = 9,
            /// <summary>
            /// 载出移载1 Z轴
            /// </summary>
            ULDTR_1_Z = 10,
            /// <summary>
            /// 载出移载2 Z轴
            /// </summary>
            ULDTR_2_Z = 11,
            /// <summary>
            /// ULDST 粗定位
            /// </summary>
            UnLoad_Cu = 12,
            /// <summary>
            /// ULDST 精定位
            /// </summary>
            UnLoad_Jing = 13,
            /// <summary>
            /// 载出移载1 Z轴
            /// </summary>
            ULDTR_1_R = 14,
            /// <summary>
            /// 载出移载2 Z轴
            /// </summary>
            ULDTR_2_R = 15,

            AlarmReset = 20,
            /// <summary>
            /// SortST 精定位当前值保存
            /// </summary>
            Locate_Jing_ZSave = 26,
            /// <summary>
            /// LaserST 上侧当前值保存
            /// </summary>
            Label_Up_ZSave = 27,
            /// <summary>
            /// LaserST 前侧当前值保存
            /// </summary>
            Label_Side_ZSave = 28,
            /// <summary>
            /// Laser 读码当前值保存
            /// </summary>
            Label_OCR_ZSave = 29,
            /// <summary>
            /// 载出移载1 Z对位点当前值保存
            /// </summary>
            ULDTR_1_ZSave = 30,
            /// <summary>
            /// 载出移载2 Z对位点当前值保存
            /// </summary>
            ULDTR_2_ZSave = 31,
            /// <summary>
            /// ULDST 粗定位当前值保存
            /// </summary>
            UnLoad_Cu_ZSave = 32,
            /// <summary>
            /// ULDST 精定位当前值保存
            /// </summary>
            UnLoad_Jing_ZSave = 33,
            /// <summary>
            /// 挑拣平台精度对位异常Pass
            /// </summary>
            Sort_Jing_NGPass = 100,
        }
    }
}
